import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()) {
            String S = sc.nextLine();

            int T = sc.nextInt();
            for(int t=0;t<T;t++) {
                int n = sc.nextInt();
                int k = sc.nextInt();
                StringBuilder sb = new StringBuilder(S);
                for(int nn=0;nn<n;nn++) {
                    sb = encoding(sb);
                }
                System.out.println(sb.charAt(k - 1));
            }
        }
    }

    public static StringBuilder encoding(StringBuilder S) {
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<S.length();i++) {
            if (S.charAt(i) == 'A') {
                sb.append("BC");
            } else if(S.charAt(i) == 'B') {
                sb.append("CA");
            } else if (S.charAt(i) == 'C') {
                sb.append("AB");
            }
        }
        return sb;
    }
}